//
//  PywCommonPywLYDataPackHelper.m
//  PywCommonSDKDemo
//
//  Created by Ampaw on 2017/8/21.
//  Copyright © 2017年 Ampaw. All rights reserved.
//

#import "PywCommonPywLYDataPackHelper.h"
#import "PywCommonSDKParameter.h"
#import "PywCommonPlatformInfo.h"
#import "PywCommonApiManager.h"
#import "PywCommonMutableDictionary.h"

@implementation PywCommonPywLYDataPackHelper

/// 包装朋友玩母包设置角色信息参数
+ (NSDictionary *)packPywLYRoleInfo:(NSDictionary *)roleInfo
{
    // 角色ID
    NSString *role_id = [roleInfo objectForKey:keyCom_GameRoleId];
    // 角色名
    NSString *role_name = [roleInfo objectForKey:keyCom_GameRoleName];
    // 角色等级
    NSString *role_level = [roleInfo objectForKey:keyCom_GameRoleLevel];
    // 区服标识
    NSString *server_id = [roleInfo objectForKey:keyCom_GameServiceId];
    // 区服完整信息
    NSString *server_name = [roleInfo objectForKey:keyCom_GameServiceName];
    
    // 参数包装
    PywCommonMutableDictionary *role_info = [PywCommonMutableDictionary dictionary];
    [role_info setObject:role_id forKey:@"roleid"];
    [role_info setObject:role_name forKey:@"roleName"];
    [role_info setObject:role_level forKey:@"roleLevel"];
    [role_info setObject:server_id forKey:@"serverArea"];
    [role_info setObject:server_name forKey:@"serverAreaName"];
    return role_info;
}

/// 包装朋友玩母包支付信息参数
+ (NSDictionary *)packPywLYPayInfo:(NSDictionary *)payInfo  paytype:(CommonPayType)paytype
{
    // 支付类型
    NSString *charge_type = [payInfo objectForKey:keyCom_PayType];
    // 商品描述
    NSString *goods_desc = [payInfo objectForKey:keyCom_GoodsDesc];
    // 商品ID，定额支付时必传，任意支付可不传
    NSString *goods_id = [payInfo objectForKey:keyCom_GoodsId];
    // 商品金额
    NSString *goods_price = [payInfo objectForKey:keyCom_GoodsPrice];
    // 游戏订单ID,默认是CP传入的游戏订单号
    NSString *order_id = [payInfo objectForKey:keyCom_GameOrderId];
    if (paytype == CommonPayType_Indie) {// 当独立使用朋友玩支付时，订单号由聚合下单接口返回
        order_id = [PywCommonPlatformInfo sharedCommonPlatformInfo].common_channel_order_sn;
    }

    // 游戏角色名
    NSString *role_name = [[payInfo objectForKey:keyCom_GameRoleInfo] objectForKey:keyCom_GameRoleName];

    // 游戏区服名
    NSString *gameServerName = [[payInfo objectForKey:keyCom_GameRoleInfo] objectForKey:keyCom_GameServiceName];
    // 存储商品信息的字典，自定义透传参数
//    PywCommonMutableDictionary *ext = [[PywCommonMutableDictionary alloc] init_WithDictionary:[payInfo objectForKey:keyCom_CustomParams]];
    NSString *ext = [payInfo objectForKey:keyCom_CustomParams];
    
    // 母包支付参数，聚合走整个母包流程
    PywCommonMutableDictionary *orderInfo = [PywCommonMutableDictionary dictionary];
    [orderInfo setObject:order_id forKey:@"order_id"];
    [orderInfo setObject:goods_desc forKey:@"gameBi"]; // 商品描述，显示在订单上面
    [orderInfo setObject:goods_price forKey:@"oriprice"];// 支付金额
    [orderInfo setObject:charge_type forKey:@"charge_type"];// 支付类型
    if ([charge_type isEqualToString:@"1"]) {// 定额支付
        [orderInfo setObject:goods_id forKey:@"cpprodid"];// 商品ID，定额支付必传,任意支付时可不传
    }
    [orderInfo setObject:role_name forKey:@"rolename"];// 游戏角色名
    [orderInfo setObject:gameServerName forKey:@"service"];// 游戏区服名
    [orderInfo setObject:ext forKey:@"cpprams"];// 自定义透传参数
    
    // 当仅使用朋友玩支付时，需要传入的参数
    if (paytype == CommonPayType_Indie) {
        [orderInfo setObject:[self channelParamsToDict:payInfo] forKey:@"channel_params"];
    }
    COMLog(@"朋友玩支付参数：orderInfo = %@",orderInfo.description);
    return orderInfo;
}

/// 当仅使用朋友玩支付时，需要传入的参数
+ (PywCommonMutableDictionary *)channelParamsToDict:(NSDictionary *)payInfo
{
    NSString *account = [PywCommonPlatformInfo sharedCommonPlatformInfo].common_account;
    NSString *token = [PywCommonPlatformInfo sharedCommonPlatformInfo].common_token;
    NSString *gamekey = [PywCommonPlatformInfo sharedCommonPlatformInfo].gamekey;
    NSString *gameid = [PywCommonPlatformInfo sharedCommonPlatformInfo].gameid;
    NSString *apisecret = [PywCommonPlatformInfo sharedCommonPlatformInfo].apisecret;
    NSString *pay_type = [payInfo objectForKey:keyCom_PayType];
    NSString *sdk_api_url = [PywCommonPlatformInfo sharedCommonPlatformInfo].pyw_channel_apiurl;
    NSString *sdk_pay_url = [PywCommonPlatformInfo sharedCommonPlatformInfo].pyw_pay_apiurl;
    PywCommonMutableDictionary *channel_params = [PywCommonMutableDictionary dictionary];
    [channel_params setObject:account forKey:@"account"];
    [channel_params setObject:token forKey:@"token"];
    [channel_params setObject:gamekey forKey:@"gamekey"];
    [channel_params setObject:gameid forKey:@"gameid"];
    [channel_params setObject:apisecret forKey:@"apisecret"];
    [channel_params setObject:pay_type forKey:@"charge_type"];
    [channel_params setObject:sdk_api_url forKey:@"sdk_api_url"];
    [channel_params setObject:sdk_pay_url forKey:@"sdk_pay_url"];
    [channel_params setObject:[PywCommonPlatformInfo sharedCommonPlatformInfo].sdk_type forKey:@"pyw_sdk_type"];
    [channel_params setObject:[PywCommonPlatformInfo sharedCommonPlatformInfo].channel_id forKey:@"th_ch_type"];
    return channel_params;
}

@end
